<?php/** * Created by PhpStorm. * User: dx * Date: 17/2/22 * Time: 下午4:06 */class UserController extends BaseController {    /**     * @Author:      dx     * @DateTime:    2017-02-22 13:43:32     * @Description: 意见反馈     */    public function feedback(){        $validator = Validator::make(Input::all(),            array(                'token' => 'required',                "content"=>'max:100',            )        );        $response = array('code' => 0, 'msg' => 'ok');        do{            if($validator->fails()) {                $response = Lang::get("error.args_error_10000");                $response["result"]=$validator->messages()->all();                break;            }            $wechat_user=$this->verificationUserToken(Input::get("token"),$response);            if(is_null($wechat_user)){                break;            }            $wechat_feedback = new WechatFeedback();            $wechat_feedback->content = Input::get('content');            $wechat_feedback->created_by = $wechat_user->id;            $wechat_feedback->save();            if(Config::get('u_meng_push.is_open_u_meng_push') ==1 && Input::get('is_app') ==1){                Orders::notifyMsg($wechat_user->id,3,'意见反馈提交成功','您的意见我们已经收到，会尽快安排人员处理，请耐心等待哦，感谢您使用安惠养车，点击查看提交内容');            }            try {                NewsLog::NewsPush($wechat_user->id,'',3,7,'意见反馈提交成功','您的意见我们已经收到，会尽快安排人员处理，请耐心等待哦，感谢您使用安惠养车，点击查看提交内容');            } catch (Exception $e) {                Log::error("error", array("system" => "api3.0" ,"event" => "feedback", 'input' =>Input::all() ,"exception" => $e));                $response = Lang::get("error.run_exception");//                break;            }            $response = array('code' => 0, 'msg' => 'ok');        }while(false);        Log::info("info", array("system" => "wechat2.0" ,"event" => "feedback", 'input' =>Input::all() ,"output" => $response));        return json_encode($response);    }    /**     * @Author:      dx     * @DateTime:    2017-02-24 10:52:29     * @Description: 用户详情     */    public function show(){        $validator = Validator::make(Input::all(),            array(                'token' => 'required',                'id'=>'required', //用户id            )        );        $response = array('code' => 0, 'msg' => 'ok');        do{            if($validator->fails()) {                $response = Lang::get("error.args_error_10000");                $response["result"]=$validator->messages()->all();                break;            }            $wechat_user=$this->verificationUserToken(Input::get("token"),$response);            if(is_null($wechat_user)){                break;            }            $id = Input::get('id');            $user = Users::where('id',$id)->first();            if($user->birthday == "0000-00-00"){                $user->birthday = "";            }            //组织返回            $response = array(                'code' => 0,'msg' => 'ok','result' => array('user' => $user,'heading_img_url' => Config::get('config.heading_img_url'),)            );        }while(false);        Log::info("info", array("system" => "wechat2.0" ,"event" => "show", 'input' =>Input::all() ,"output" => $response));        return json_encode($response);    }    /**     * @Author:      dx     * @DateTime:    2017-02-24 11:26:29     * @Description: 编辑 保存 用户     */    public function update(){        $validator = Validator::make(Input::all(),            array(                'token' => 'required',                'id' => 'required',                // 'phone'=>'required|unique:accounts,phone,'.Input::get('id'),            )        );        $response = array('code' => 0, 'msg' => 'ok');        do{            if($validator->fails()) {                $response = Lang::get("error.args_error_10000");                $response["result"]=$validator->messages()->all();                break;            }            $wechat_user=$this->verificationUserToken(Input::get("token"),$response);            if(is_null($wechat_user)){                break;            }            $users=Users::find(Input::get('id'));            if(is_null($users)){                $response = Lang::get("error.user_not_exit_21001");                break;            }            $heading_img = Input::get('heading_img')?Input::get('heading_img'):"";            if($heading_img){                if($users->heading_img == $heading_img){                    $pic_name = $heading_img;                }else{                    $pic_name = date("YmdHis", time()) . rand(10000, 99999) . '.png';                    $img = base64_decode($heading_img);                    $path=Config::get('config.heading_img');                    File::put( $path . $pic_name, $img);                }                $users->heading_img = $pic_name;            }else{                $users->heading_img = $heading_img;            }            try {                if(Input::get('birthday')){                    $users->birthday =  Input::get('birthday');                }                $users->nickname =  Input::get('nickname');                $users->sex =  Input::get('sex',0);                $users->real_name =  Input::get('real_name');                $users->updated_at = date('Y-m-d H:i:s',time());                $users->save();                $response = array(                    'code' => 0,                    'msg' => 'ok',                    "result" => array(                        'heading_img_url' => Config::get('config.heading_img_url'),                        'heading_img' => $users->heading_img,                        'nickname' => $users->nickname,                    )                );            } catch (Exception $e) {                Log::error("error", array("system" => "wechat2.0" ,"event" => "update", 'input' =>Input::all() ,"exception" => $e));                $response = Lang::get("error.user_save_error_21007");                break;            }        }while(false);        Log::info("info", array("system" => "wechat2.0" ,"event" => "update", 'input' =>Input::all() ,"output" => $response));        return json_encode($response);    }    /*    * @Author:      kc    * @DateTime:    2017-02-24 10:18:32    * @Description: 优惠券    * */    public function myCoupon(){        $validator = Validator::make(Input::all(),            array(                'token' => 'required',                'status' => 'required',            )        );        do{            if($validator->fails()){                $response = Lang::get('error.args_error_10000');                $response["result"]=$validator->messages()->all();                break;            }            $wechat_user = $this->verificationUserToken(Input::get('token'),$response);            if(is_null($wechat_user)){                break;            }            if(Input::get('status') == 1){                $status = 2;            }else{                $status = 6;            }            $coupons = Coupons::getCouponsList($wechat_user->id,$status);            $coupons_list = array();            if(!is_null($coupons)){                foreach($coupons as $v){                    $coupon_base_info = CouponBaseInfo::getCouponBaseInfo($v->coupon_base_info_id);                    if(!is_null($coupon_base_info)){                        $_coupon = array();                        if($coupon_base_info->date_type == 1){                            $begin_date = date('Y-m-d',strtotime($v->get_time));                            $end_date = $v->end_date;                        }else{                            $begin_date = $coupon_base_info->begin_date;                            $end_date = $coupon_base_info->end_date;                        }                        if((int)Input::get('status') === 1 && strtotime(Common::currentTimeString()) - strtotime($end_date.' 23:59:59') > 0){                            continue;                        }                        if($coupon_base_info->limit_type == 2){                            $pack_id = CouponPackLimit::where('coupon_base_info_id',$coupon_base_info->id)->lists('pack_id');                            $product_pack= ProductPackage::whereIn('id',$pack_id)->lists('name');;                        }else{                            $product_pack = array();                        }                        $_coupon["id"] = $v->id;                        $_coupon['code'] = $v->code;                        $_coupon['amount'] = bcdiv($coupon_base_info->amount,100,2);                        $_coupon['name'] = $coupon_base_info->name;                        $_coupon['limit_amount'] =  bcdiv($coupon_base_info->limit_amount,100,2);                        $_coupon['begin_date'] = $begin_date;                        $_coupon['end_date'] = $end_date;                        $_coupon['coupon_type'] = $coupon_base_info->type;                        $_coupon['type'] = Coupons::$type[$coupon_base_info->type];                        $_coupon['status'] = Coupons::$statsu[$v->status];                        $_coupon['limit_order'] = $coupon_base_info->limit_order;                        $_coupon['limit_car'] = $coupon_base_info->limit_car;                        $_coupon['used_car_no'] = $v->used_car_no;                        $_coupon["phone"] = $wechat_user->phone;                        $_coupon["discount"] = $coupon_base_info->discount;                        $_coupon["limit_book"] = $coupon_base_info->limit_book;                        $_coupon["product_pack"] = $product_pack;                        $_coupon["limit_type"] = $coupon_base_info->limit_type;                        $_coupon["coupon_base_info_id"] = $coupon_base_info->id;                        array_push($coupons_list, $_coupon);                    }                }            }            $response = array(                'code' => 0,                'msg' => 'ok',                'result' => $coupons_list,            );        }while(false);        Log::info("info", array("system" => "wechat2.0" ,"event" => "myCoupon", 'input' =>Input::all() ,"output" => $response));        return json_encode($response);    }    /*      * @Author:      kc      * @DateTime:    2017-03-31 14:22:10      * @Description: 优惠券兑换      * */    public function exchangeCoupon(){        $validator = Validator::make(Input::all(),            array(                'token' => 'required',                'code' => 'required',            )        );        do{            if($validator->fails()){                $response = Lang::get('error.args_error_10000');                $response["result"]=$validator->messages()->all();                break;            }            $wechat_user = $this->verificationUserToken(Input::get('token'),$response);            if(is_null($wechat_user)){                break;            }            $coupons = Coupons::where('code',trim(Input::get('code')))->first();            if(empty($coupons)){                $response = Lang::get('error.redeem_code_invalid_error_71006');                break;            }            $coupon_base_info = CouponBaseInfo::where('id',$coupons->coupon_base_info_id)->first();            if(empty($coupon_base_info)){                $response = Lang::get('error.redeem_code_invalid_error_71006');                break;            }            if($coupon_base_info->code_type == 1){                if($coupons->received_user_id){                    $response = Lang::get('error.redeem_code_user_error_71008');                    break;                }            }            if($coupon_base_info->limit_num != 0){                $coupons_num = Coupons::where('coupon_base_info_id',$coupon_base_info->id)->where('received_user_id',$wechat_user->id)->count();                if($coupons_num >= $coupon_base_info->limit_num){                    $response = Lang::get('error.redeem_code_invalid_or_game_over_71007');                    break;                }            }            if($coupon_base_info->code_type == 2){                $coupons_two = Coupons::where('code',Input::get('code'))->where('received_user_id',0)->first();                if(empty($coupons_two)){                    $response = Lang::get('error.redeem_code_invalid_error_71006');                    break;                }                $coupons_two_id = $coupons_two->id;            }            if($coupon_base_info->status == 0){                $response = Lang::get('error.redeem_code_invalid_error_71006');                break;            }            $time = date("Y-m-d H:i:s",time());            if($coupon_base_info->date_type == 0 && $coupon_base_info->begin_date.' 00:00:00' > $time){                $response = Lang::get('error.coupon_exchange_time_error_70001');                break;            }            if($coupon_base_info->date_type == 0 &&$coupon_base_info->end_date.' 23:59:59' < $time){                $response = Lang::get('error.redeem_code_game_over_71005');                break;            }            try{                $coupons_id = isset($coupons_two_id)?$coupons_two_id:$coupons->id;                $coupon_edit = Coupons::find($coupons_id);                $coupon_edit->received_user_id = $wechat_user->id;                $coupon_edit->status = 2;                if($coupon_base_info->date_type == 1){                    $coupon_edit->end_date = date("Y-m-d", strtotime("+" . $coupon_base_info->date_num . " day"));                }                $coupon_edit->get_time = Common::currentTimeString();                $coupon_edit->save();            }catch (Exception $e){                Log::error("error", array("system" => "wechat2.0" ,"event" => "exchangeCoupon", 'input' =>Input::all() ,"exception" => $e));                $response = Lang::get("error.coupon_exchange_error_70003");                break;            }            $response = array(                "code" => 0,                "msg" => "ok",            );            //ActivityCouponInfo::where('coupon_base_info_id',$coupons->coupon_base_info_id)->groupBy('activity_id')->get();        }while(false);        Log::info("info", array("system" => "wechat2.0" ,"event" => "exchangeCoupon", 'input' =>Input::all() ,"output" => $response));        return json_encode($response);    }    /**     * @Author:      dx     * @DateTime:    2017-02-28 10:26:29     * @Description: 用户车库     */    public function garage() {        $validator = Validator::make(Input::all(),            array(                'token' => 'required',            )        );        do{            if($validator->fails()){                $response = Lang::get('error.args_error_10000');                $response["result"]=$validator->messages()->all();                break;            }            $wechat_user = $this->verificationUserToken(Input::get('token'),$response);            if(is_null($wechat_user)){                break;            }            $user_car_temp_info = UserCarInfo::getUserCarInfoSort($wechat_user->id);            //        print_r($user_car_temp_info);exit;            $user_car_info = array();            foreach($user_car_temp_info as $k => $u_c_t_i){                if(!$u_c_t_i->car_model_id){                    continue;                }                if($u_c_t_i->is_lock == 1){                    $user_auth_car = UserCarInfo::userAuthCar($u_c_t_i->car_no);                    if(!$user_auth_car){                        continue;                    }                    $user_car_info[$k] = (array)UserCarInfo::getCarStandardInfo($user_auth_car->car_model_id);                }else{                    $user_car_info[$k] = (array)UserCarInfo::getCarStandardInfo($u_c_t_i->car_model_id);                }                $user_car_info[$k]['car_no'] = $u_c_t_i->car_no;                $user_car_info[$k]['user_car_info_id'] = $u_c_t_i->user_car_info_id;                $user_car_info[$k]['is_default'] = $u_c_t_i->is_default;                $user_car_info[$k]['is_lock'] = $u_c_t_i->is_lock;                $user_car_info[$k]['car_brand_img_url'] = Config::get('config.car_brand_img_url');            }            $response = array(                'code' => 0,                'msg' => 'ok',                'result' => array('user_car_info' =>array_merge($user_car_info)),            );        }while(false);        Log::info("info", array("system" => "wechat2.0" ,"event" => "garage", 'input' =>Input::all() ,"output" => $response));        return json_encode($response);    }    /*      * @Author:      kc      * @DateTime:    2017-03-01 14:52:32      * @Description: 城市列表      * */    public function locationCity(){        $resource_city = ResourceCityType::where('status',1);        if(Input::get('city_name')){            $resource_city->where('name','like','%'.Input::get('city_name').'%');        }        $data = $resource_city->select([            'id',            'name',            'short_name',            'area',            'lat',            'lon',        ])            ->get();        $response = array(            'code' => 0,            'msg' => 'ok',            'result' => $data,        );        Log::info("info", array("system" => "wechat2.0" ,"event" => "locationCity", 'input' =>Input::all() ,"output" => $response));        return json_encode($response);    }    /*     * @Author:      dx     * @DateTime:    2017-03-28 10:52:32     * @Description:领取优惠券     * 1.判断活动是否存在     * 2.判断用户保险单，如果商业险金额超过2000 可领取     * 3.判断用户是否重复领取     * 4.判断优惠券是否发放完     * */    public function receive(){        $validator = Validator::make(Input::all(),            array(                "token" => "required",//                "activity_id" => "required|integer", //具体活动的ID                "car_no" => "required", //车牌号            )        );        $response = array('code' => 0, 'msg' => 'ok','result' => array());        do{            if($validator->fails()) {                $response = Lang::get("error.args_error_10000");                $response["result"]=$validator->messages()->all();                break;            }            $wechat_user=$this->verificationUserToken(Input::get("token"),$response);            if(is_null($wechat_user)){                break;            }            //根据活动查到活动有效期            $activity_dateline = Coupons::getActivityBaseInfo(Config::get('c_config.ins_activity_id'));            if(!$activity_dateline){                $response = Lang::get('error.activity_not_exist');                break;            }            //根据活动期间查找合适的保险单            $insu = Insurance::where('car_no',Input::get('car_no'))->where('status',1)->whereBetween('bill_time',array($activity_dateline->begin_date.':00',$activity_dateline->end_date.':59'))->orderBy('id','desc')->get();            if(is_null($insu)){                $response = Lang::get('error.insu_not_exit_error_60000');                break;            }            $in="";            foreach($insu as $_i){                $insu_detail = InsuranceDetail::where('insurance_id',$_i->id)->where('type',2)->first();                if(is_null($insu_detail)){                    continue;                }                if($insu_detail->amount < 200000){                    continue;                }                if($insu_detail->car_damage == 0){                    continue;                }                $in = $_i->id;            }            if($in==""){                $response = Lang::get('error.insu_not_exit_error_60000');                break;            }            $insu = Insurance::where('id',$in)->first();            if(Config::get('c_config.ins_activity_id')) {                $coupon_res = Coupons::getCouponInfo($wechat_user->id,Config::get('c_config.ins_activity_id'),$insu->bill_time,$response);                if(is_null($coupon_res)){                    break;                }                $_coupon = array();                $_coupon_list = array();                foreach ($coupon_res as  $_c) {                    $coupon_base_info =  Coupons::getCouponBaseInfo($_c->coupon_base_info_id);                    if(!$coupon_base_info){                        $response = Lang::get('error.coupon_not_exit_error_51004');                        break;                    }                    if($coupon_base_info->status == 0){                        $response = Lang::get('error.coupon_status_error_51005');                        break;                    }                    if($coupon_base_info->date_type == 0){ //起止时间                        if(strtotime($coupon_base_info->end_date.' 23:59:59') < strtotime(date('Y-m-d H:i:s',time()))){                            $response = Lang::get('error.coupon_game_over_error_51006');                            break;                        }                    }                    $coupons = Coupons::getCoupons($_c->coupon_base_info_id,$wechat_user->id,Input::get('car_no'));                    if($coupons){                        $response = Lang::get('error.coupon_received_error_51007');                        break;                    }                    $check_car_no = Coupons::where('used_car_no',Input::get('car_no'))->first();                    if($check_car_no){                        $response = Lang::get('error.coupon_received_error_51007');                        break;                    }                    $coupon_has_been_brought_out = Coupons::getCouponOther($_c->coupon_base_info_id);                    if(is_null($coupon_has_been_brought_out)){                        $response = Lang::get("error.coupon_has_been_brought_out_51008");                        break;                    }                    $end_date = null;                    if($coupon_base_info->date_type == 1){ //顺延日期                        $end_date = date("Y-m-d",strtotime("+".$coupon_base_info->date_num." day"));                    }                    $status = DB::update("update coupons set received_user_id =? ,get_time=?,status = 2,end_date=?,used_car_no=? where coupon_base_info_id=? and get_time ='0000-00-00 00:00:00' limit 1", array(                        $wechat_user->id, Common::currentTimeString(),$end_date,strtoupper(Input::get('car_no')), $_c->coupon_base_info_id                    ));                    if (!$status) {                        $response = Lang::get("error.coupon_receive_not_success_error_51009");                        break;                    }                    if($coupon_base_info->date_type == 1){                        $begin_date = date('Y-m-d',time());                        $end_date = date("Y-m-d",strtotime("+".$coupon_base_info->date_num." day"));                    }else{                        $begin_date = $coupon_base_info->begin_date;                        $end_date  = $coupon_base_info->end_date;                    }                    $_coupon['begin_date'] = $begin_date;                    $_coupon['end_date'] = $end_date;                    $_coupon['name'] = $coupon_base_info->name;                    $_coupon['limit_amount'] = $coupon_base_info->limit_amount;                    $_coupon['amount'] = $coupon_base_info->amount;                    $_coupon['type'] = Coupons::$type[$coupon_base_info->type];                    $_coupon['limit_order'] = $coupon_base_info->limit_order;                    array_push($_coupon_list, $_coupon);                }                $response["result"] = array(                    "coupon_info" => $_coupon_list                );            } else {                $response = Lang::get("error.coupon_receive_not_success");                break;            }        }while(false);        Log::info("info", array("system" => "wechat2.0" ,"event" => "receive", 'input' =>Input::all() ,"output" => $response));        return json_encode($response);    }    /*     * @Author:      dx     * @DateTime:    2017-04-01 13:58:32     * @Description: 反馈意见列表     * */    public function feedBackList(){        $validator = Validator::make(Input::all(),            array(                'token' => 'required',            )        );        do{            if($validator->fails()){                $response = Lang::get('error.args_error_10000');                $response["result"]=$validator->messages()->all();                break;            }            $wechat_user = $this->verificationUserToken(Input::get('token'),$response);            if(is_null($wechat_user)){                break;            }            $feedBackList = WechatFeedback::where('type',1)->where('created_by',$wechat_user->id)->orderBy('created_at','desc')->get();            $response = array(                'code' => 0,'msg' => 'ok', 'result' => $feedBackList,            );        }while(false);        Log::info("info", array("system" => "wechat2.0" ,"event" => "feedBackList", 'input' =>Input::all() ,"output" => $response));        return json_encode($response);    }    /*   * @Author:      kc   * @DateTime:    2017-04-01 15:14:29   * @Description: 新用户注册活动   * */    public function newUser(){        $validator = Validator::make(Input::all(),            array(                'phone' => 'required',                'check_code' => 'required',                "activity_id" => "required|integer",                'register_source_id' => 'required',            )        );        do {            if ($validator->fails()) {                $response = Lang::get("error.args_error_10000");                $response["result"] = $validator->messages()->all();                break;            }            $area = '010';            $record = UserCheckCode::getCheckCode(Input::get('phone'), $area);            if (is_null($record)) { //没有记录                $response = Lang::get("error.user_login_checkcode_null_10004");                break;            }            //校验是否超过时限            if (time() > Common::datetime2Num($record->out_time_at)) {                $response = Lang::get("error.user_login_checkcode_timeout_10005");                break;            }            //校验验证码是否正确            if ($record->check_code != Input::get('check_code')) {                $response = Lang::get("error.user_login_checkcode_error_10006");                break;            }            //校验活动时间            $activity = Coupons::getNewUserRegister(Input::get('activity_id'),$response);            if(is_null($activity)){                break;            }            $users = Users::getUser(Input::get('phone'));            if (is_null($users)) {                try{                    $users = new Users();                    $users->phone = Input::get('phone');                    $users->status = 1;                    $users->register_source_id = Input::get('register_source_id');                    $users->real_name = '';                    $users->sex = 0;                    $users->area = '010';                    $users->type = 1;                    $users->nickname = '';                    $users->heading_img = '';                    $users->activate = 1;                    $users->member = Config::get('user.def_member');                    $users->save();                }catch (Exception $e){                    Log::error("error", array("system" => "wechat2.0" ,"event" => "newUser", 'input' =>Input::all() ,"exception" => $e));                    $response = Lang::get("error.data_save_error_30001");                    break;                }            }else{                //type 1个人 2企业                //如果发现此用户是老用户，且激活 提示                if($users->type == 1 && $users->activate == 1){                    $response = Lang::get('error.coupon_user_exist_error_71004');                    break;                }else{                    $users->activate = 1;                    $users->save();                }            }            try{                foreach ($activity as  $v) {                    $coupon_base_info = Coupons::getCouponBaseInfo($v->coupon_base_info_id);                    if (!$coupon_base_info) {                        $response = Lang::get('error.coupon_not_exit_error_51004');                        break;                    }                    if ($coupon_base_info->status == 0) {                        $response = Lang::get("error.coupon_status_error_51005");                        break;                    }                    if ($coupon_base_info->date_type == 0) { //起止时间                        if (strtotime($coupon_base_info->end_date.' 23:59:59') < strtotime(date('Y-m-d H:i:s', time()))) {                            $response = Lang::get('error.coupon_game_over_error_51006');                            break;                        }                    }                    $coupon_whether_exist = Coupons::getCouponOther($v->coupon_base_info_id);                    if (is_null($coupon_whether_exist)) {                        $response = Lang::get("error.coupon_has_been_brought_out_51008");                        break;                    }                    $coupons = Coupons::find($coupon_whether_exist->id);                    $coupons->status = 2;                    $coupons->received_user_id = $users->id;                    $coupons->get_time = date("Y-m-d H:i:s", time());                    if ($coupon_base_info->date_type == 1) { //顺延日期                        $coupons->end_date = date("Y-m-d", strtotime("+" . $coupon_base_info->date_num . " day"));                    }                    $coupons->save();                }                try{                    $token="";                    $find_token = UserTokens::where('user_id',$users->id)->first();                    if(!is_null($find_token)){ //有token 判断token是否过期                        if(strtotime($find_token->out_time_at) < time()){//说明过期了                            UserTokens::where('user_id',$users->id)->delete();                            $user_token = new UserTokens;                            $user_token->user_id = $users->id;                            $user_token->token = md5(Input::get('phone').time().rand(0,100));                            $user_token->created_at = Common::num2DateTime(time());                            $user_token->out_time_at = Common::num2DateTime(time() + Config::get("user.token_time_out"));                            $user_token->save();                            $token=$user_token->token;                        }else{                            $token=$find_token->token;                        }                    }else{                        $user_token = new UserTokens;                        $user_token->user_id = $users->id;                        $user_token->token = md5(Input::get('phone').time().rand(0,100));                        $user_token->created_at = Common::num2DateTime(time());                        $user_token->out_time_at = Common::num2DateTime(time() + Config::get("user.token_time_out"));                        $user_token->save();                        $token=$user_token->token;                    }                } catch (Exception $e){                    Log::error("error", array("system" => "wechat2.0" ,"event" => "newUser", 'input' =>Input::all() ,"exception" => $e));                    $response = Lang::get("error.login_create_token_error_21005");                    break;                }                $response = array(                    'code' => 0,                    'msg' => 'ok',                    'result' =>array(                        'id'=> $users->id,                        'token' => $token,                        'phone' => $users->phone,                        'real_name' => $users->real_name,                        'sex' => $users->sex,                        'area' => $users->area,                        'remark' => $users->remark,                        'type' => $users->type,                        'nickname' => $users->nickname,                        'birthday' => $users->birthday,                        'heading_img' => $users->heading_img,                    )                );            }catch (Exception $e){                Log::error("error", array("system" => "wechat2.0" ,"event" => "newUser", 'input' =>Input::all() ,"exception" => $e));                $response = Lang::get("error.data_save_error_30001");                break;            }        }while(false);        Log::info("info", array("system" => "wechat2.0" ,"event" => "newUser", 'input' =>Input::all() ,"output" => $response));        return json_encode($response);    }    /**     * @Author:      dx     * @DateTime:    2017-04-05 16:03:32     * @Description: 分享     */    public function share(){        $validator = Validator::make(Input::all(),            array(                "url"=>'required',            )        );        $response = array('code' => 0, 'msg' => 'ok');        do{            if($validator->fails()) {                $response = Lang::get("error.args_error_10000");                $response["result"]=$validator->messages()->all();                break;            }            $shareSet = ['title'=>Config::get('config.default_share_title'),                'url'=> Config::get('config.default_share_url'),                'image'=>Config::get('config.default_share_img'),                'desc'=>Config::get('config.default_share_desc')];//    分享默认值            $url = Input::get('url');            $res = strpos($url,'from');            if($res !== false){                $temp_url = explode('?',$url);                $url = $temp_url[0];            }            $wechat_share_data = WechatShareData::where('wechat_url',$url)->first();            if(!is_null($wechat_share_data)){                $shareSet['title'] = $wechat_share_data->tittle;                $shareSet['url'] = Config::get('config.redirect_url').$wechat_share_data->share_url;                $shareSet['desc'] = $wechat_share_data->desc;                $shareSet['image'] = Config::get('config.share_img_url').$wechat_share_data->img_url;            }            $response = array('code' => 0, 'msg' => 'ok','shareSet' => $shareSet);        }while(false);        Log::info("info", array("system" => "wechat2.0" ,"event" => "share", 'input' =>Input::all() ,"output" => $response));        return json_encode($response);    }    /**     * @Author:      kc     * @DateTime:    2017-05-16 13:50:53     * @Description: 账户余额     * @link:     */    public function remainingSum(){        $validator = Validator::make(Input::all(),            array(                "token" => "required",            )        );        do{            if($validator->fails()){                $response = Lang::get("error.args_error_10000");                $response["result"]=$validator->messages()->all();                break;            }            $wechat_user=$this->verificationUserToken(Input::get("token"),$response);            if(is_null($wechat_user)){                break;            }            $user_account = UserAccount::where('user_id',$wechat_user->id)->first();            $remaining_sum = isset($user_account->balance)?bcdiv($user_account->balance,100,2):'0.00';            $recharge_record = DB::table('acount_user_recharge_record')                ->select([                    'amount',                    'created_at',                    'pay_type'                ])                ->where('user_id',$wechat_user->id)                ->whereIn('status',array(1,6))                ->get();            foreach($recharge_record as $k => $v){                $v->type = AcountUserRecharge::$pay_type_name[$v->pay_type];                $v->record_type = 1;                $v->is_wash = 0;                $v->amount = "+".bcdiv($v->amount,100,2);            }            $spend_record = DB::table('acount_user_recharge_spend_record')                ->select([                    'type',                    'amount',                    'tran_no',                    'created_at',                    'buss_id'                ])                ->where("user_id",$wechat_user->id)                ->whereIn('type',array(1,2,3,4,5,6,7))                ->where('status',1)                ->get();            foreach($spend_record as $k => $v){                if($v->type == 1){                    $pay_record = OrderPayRecord::find($v->buss_id);                    if(!empty($pay_record)){                        $v->record_type = 2;                        $v->type = "订单消费";                        $v->is_wash = 0;                        $orders = Orders::where('order_id',$pay_record->order_id)->first();                        if($orders->order_type == 1){                            $v->order_id = $pay_record->order_id;                        }                        $v->amount = "-".bcdiv($v->amount,100,2);                        array_push($recharge_record,$v);                    }                }                if(in_array($v->type,array(3,4,5,6,7,8))){                    $v->record_type = 2;                    $v->type = AcountUserRecharge::$recharge_type[$v->type];                    $v->is_wash = 0;                    $v->amount = "-".bcdiv($v->amount,100,2);                    array_push($recharge_record,$v);                }//                if($v->type == 3){//                    $v->record_type = 2;//                    $v->type = "撤销充值消费";//                    $v->is_wash = 0;//                    $v->amount = "-".bcdiv($v->amount,100,2);//                    array_push($recharge_record,$v);//                }                if($v->type == 2){                    $pay_record = CarWashOrderPayRecord::find($v->buss_id);                    $v->record_type = 2;                    $v->type = "洗车单消费";                    $v->is_wash = 1;                    $v->order_id = $pay_record->order_id;                    $v->amount = "-".bcdiv($v->amount,100,2);                    array_push($recharge_record,$v);                }//                if($v->type == 4){//                    $v->record_type = 2;//                    $v->type = "退款消费";//                    $v->is_wash = 0;//                    $v->amount = "-".bcdiv($v->amount,100,2);//                    array_push($recharge_record,$v);//                }//                if($v->type == 6){//                    $v->record_type = 2;//                    $v->type = "余额调整消费";//                    $v->is_wash = 0;//                    $v->amount = "-".bcdiv($v->amount,100,2);//                    array_push($recharge_record,$v);//                }            }            $wasorder_record = ReturnOrderRecord::join('orders','orders.order_id','=','return_order_record.return_order_id')                ->select([                    'return_order_record.return_order_id as order_id',                    'return_order_record.amount',                    'return_order_record.created_at'                ])                ->where('orders.user_id',$wechat_user->id)                ->where("return_order_record.pay_type",8)                ->get();            foreach($wasorder_record as $k => $v){                $v->type = "退款";                $v->is_wash = 0;                $v->record_type = 1;                $v->amount = "+".bcdiv($v->amount,100,2);                array_push($recharge_record,$v);            }            $spendre_record = AcountUserRechargeSpendRe::select([                'type',                'amount',                'tran_no',                'created_at'            ])                ->where("user_id",$wechat_user->id)                ->where('status',1)                ->get();            foreach($spendre_record as $v){//                if($v->type == 1){//                    $v->record_type = 1;//                    $v->type = "订单退款";//                    $v->is_wash = 0;//                    $v->amount = "+".bcdiv($v->amount,100,2);//                    array_push($recharge_record,$v);//                }                if($v->type == 2){                    $v->record_type = 1;                    $v->type = "洗车单退款";                    $v->is_wash = 1;                    $v->amount = "+".bcdiv($v->amount,100,2);                    array_push($recharge_record,$v);                }                if(in_array($v->type,array(1,3,4))){                    $v->record_type = 1;                    $v->type = AcountUserRecharge::$rechargere_type[$v->type];                    $v->is_wash = 0;                    $v->amount = "+".bcdiv($v->amount,100,2);                    array_push($recharge_record,$v);                }            }            $data = json_decode(json_encode($recharge_record),true);            $sorts = array(                'direction' => "SORT_DESC", //排序顺序标志 SORT_DESC 降序；SORT_ASC 升序                'field'     => "created_at",       //排序字段            );            if(!empty($data)){                $arrSort = array();                foreach($data AS $uniqid => $row){                    foreach($row AS $key=>$value){                        $arrSort[$key][$uniqid] = $value;                    }                }                if($sorts['direction']){                    array_multisort($arrSort[$sorts['field']], constant($sorts['direction']), $data);                }            }            $response = array(                "code" => 0,                "msg" => "ok",                "result" => array(                    'remaining_sum' => $remaining_sum,                    'record' => $data,                )            );        }while(false);        Log::info("info", array("system" => "wechat2.0" ,"event" => "remainingSum", 'input' =>Input::all() ,"output" => $response));        return json_encode($response);    }    /**     * @Author:      kc     * @DateTime:    2017-05-16 15:16:53     * @Description: 我的信息     * @link：     */    public function myInformation(){        $validator = Validator::make(Input::all(),            array(                "token" => "required",            )        );        do{            if($validator->fails()){                $response = Lang::get("error.args_error_10000");                $response["result"]=$validator->messages()->all();                break;            }            $wechat_user=$this->verificationUserToken(Input::get("token"),$response);            if(is_null($wechat_user)){                break;            }            //未读消息累加            $is_read_count = UmengPushLog::where('is_read',1)->where('field_name','user_id')->where('field_value',$wechat_user->id)->where('status',UmengPushLog::STATUS_RECEIVED)->count();            $coupon_is_read_count = UmengPushLog::where('is_read',1)->where('field_name','user_id')->where('type',2)->where('field_value',$wechat_user->id)->where('status',UmengPushLog::STATUS_RECEIVED)->count();//2-优惠券            $feed_is_read_count = UmengPushLog::where('is_read',1)->where('field_name','user_id')->where('type',3)->where('field_value',$wechat_user->id)->where('status',UmengPushLog::STATUS_RECEIVED)->count();//3-反馈            $money_is_read_count = UmengPushLog::where('is_read',1)->where('field_name','user_id')->where('type',4)->where('field_value',$wechat_user->id)->where('status',UmengPushLog::STATUS_RECEIVED)->count();//4-余额            $user_account = UserAccount::where('user_id',$wechat_user->id)->first();            $to_be_confirmed = Orders::where('user_id',$wechat_user->id)->whereIn('status',Orders::$order_status[2])->where('order_type',1)->count();            $confirmed = Orders::where('user_id',$wechat_user->id)->whereIn('status',Orders::$order_status[3])->where('order_type',1)->count();            $wait_evaluate = Orders::where('user_id',$wechat_user->id)->whereIn('status',Orders::$order_status[4])->where('order_type',1)->count();            $coupons = Coupons::getCouponsList($wechat_user->id,2);            $coupons_count = count($coupons);            if(!is_null($coupons)){                foreach($coupons as $v) {                    $coupon_base_info = CouponBaseInfo::getCouponBaseInfo($v->coupon_base_info_id);                    if(empty($coupon_base_info)){                        $coupons_count = $coupons_count-1;                    }else{                        if($coupon_base_info->date_type == 1){                            $end_date = $v->end_date;                        }else{                            $end_date = $coupon_base_info->end_date;                        }                        if($v->status === 2 && strtotime(Common::currentTimeString()) - strtotime($end_date.' 23:59:59') > 0){                            $coupons_count = $coupons_count-1;                        }                    }                }            }            $response = array(                "code" => 0,                "msg" => "ok",                "result" => array(                    "remaining_sum" => isset($user_account->balance)?bcdiv($user_account->balance,100,2):'0.00',                    "to_be_confirmed" => $to_be_confirmed,                    "confirmed" => $confirmed,                    "coupons_count" => $coupons_count,                    "wait_evaluate" => $wait_evaluate,                    "is_read_count" => $is_read_count,                    "coupon_is_read_count" => $coupon_is_read_count,                    "feed_is_read_count" => $feed_is_read_count,                    "money_is_read_count" => $money_is_read_count,                )            );        }while(false);        Log::info("info", array("system" => "wechat2.0" ,"event" => "myInformation", 'input' =>Input::all() ,"output" => $response));        return json_encode($response);    }    /*     * @Author:      dx     * @DateTime:    2017-07-19 16:52:32     * @Description:tang shan 活动领取优惠券     * */    public function coupon_receive(){        $validator = Validator::make(Input::all(),            array(                "phone" => "required",//                "activity_id" => "required|integer", //具体活动的ID                "check_code" => "required",                'register_source_id' => 'required',            )        );        $response = array('code' => 0, 'msg' => 'ok','result' => array());        do{            if($validator->fails()) {                $response = Lang::get("error.args_error_10000");                $response["result"]=$validator->messages()->all();                break;            }            $area = '010';            $record = UserCheckCode::getCheckCode(Input::get('phone'),$area);            if (is_null($record)) { //没有记录                $response = Lang::get("error.user_login_checkcode_null_10004");                break;            }            //校验是否超过时限            if (time() > Common::datetime2Num($record->out_time_at)) {                $response = Lang::get("error.user_login_checkcode_timeout_10005");                break;            }            //校验验证码是否正确            if ($record->check_code != Input::get('check_code')) {                $response = Lang::get("error.user_login_checkcode_error_10006");                break;            }            $wechat = Users::getUser(Input::get('phone'));            if (is_null($wechat)) {                try{                    $wechat = new Users();                    $wechat->phone = Input::get('phone');                    $wechat->status = 1;                    $wechat->register_source_id = Input::get('register_source_id');//1-微站 2-app 14-门店                    $wechat->real_name = '';                    $wechat->sex = 0;                    $wechat->area = '010';                    $wechat->type = 1;                    $wechat->activate = 1;                    $wechat->nickname = '';                    $wechat->heading_img = '';                    $wechat->member = Config::get('user.def_member');                    $wechat->save();                    //新用户注册用券                    if(Config::get("config.register_coupons") == 1){                        $activity = Coupons::getNewUserRegister(Config::get('config.activity_id'),$response);                        if(!is_null($activity)){                            foreach ($activity as  $v) {                                $coupon_base_info = Coupons::getCouponBaseInfo($v->coupon_base_info_id);                                if (!$coupon_base_info) {                                    continue;                                }                                if ($coupon_base_info->status == 0) {                                    continue;                                }                                if ($coupon_base_info->date_type == 0 && strtotime($coupon_base_info->end_date.' 23:59:59') < strtotime(date('Y-m-d H:i:s', time()))) { //起止时间                                    continue;                                }                                $coupon_whether_exist = Coupons::getCouponOther($v->coupon_base_info_id);                                if (!is_null($coupon_whether_exist)) {                                    $coupons = Coupons::find($coupon_whether_exist->id);                                    $coupons->status = 2;                                    $coupons->received_user_id = $wechat->id;                                    $coupons->get_time = date("Y-m-d H:i:s", time());                                    if ($coupon_base_info->date_type == 1) { //顺延日期                                        $coupons->end_date = date("Y-m-d", strtotime("+" . $coupon_base_info->date_num . " day"));                                    }                                    $coupons->save();                                }                            }                        }                    }                }catch (Exception $e){                    Log::error("error", array("system" => "wechat2.0" ,"event" => "coupon_receive", 'input' =>Input::all() ,"exception" => $e));                    $response = Lang::get("error.login_create_token_error_21005");                    break;                }            }            if($wechat->status == 2){                $response = Lang::get('error.user_frozen_error_21002');                break;            }            //如果发现此用户是老用户，且未激活 直接激活            if($wechat->type == 1 && $wechat->activate == 0){                //注册送券                if(Config::get("config.register_coupons") == 1){                    $activity = Coupons::getNewUserRegister(Config::get('config.activity_id'),$response);                    if(!is_null($activity)){                        foreach ($activity as  $v) {                            $coupon_base_info = Coupons::getCouponBaseInfo($v->coupon_base_info_id);                            if (!$coupon_base_info) {                                continue;                            }                            if ($coupon_base_info->status == 0) {                                continue;                            }                            if ($coupon_base_info->date_type == 0 && strtotime($coupon_base_info->end_date.' 23:59:59') < strtotime(date('Y-m-d H:i:s', time()))) { //起止时间                                continue;                            }                            $coupon_whether_exist = Coupons::getCouponOther($v->coupon_base_info_id);                            if (!is_null($coupon_whether_exist)) {                                $coupons = Coupons::find($coupon_whether_exist->id);                                $coupons->status = 2;                                $coupons->received_user_id = $wechat->id;                                $coupons->get_time = date("Y-m-d H:i:s", time());                                if ($coupon_base_info->date_type == 1) { //顺延日期                                    $coupons->end_date = date("Y-m-d", strtotime("+" . $coupon_base_info->date_num . " day"));                                }                                $coupons->save();                            }                        }                    }                }                $wechat->activate = 1;                $wechat->save();            }            try{                $token="";                $find_token = UserTokens::where('user_id',$wechat->id)->first();                if(!is_null($find_token)){ //有token 判断token是否过期                    if(strtotime($find_token->out_time_at) < time()){//说明过期了                        UserTokens::where('user_id',$wechat->id)->delete();                        $user_token = new UserTokens;                        $user_token->user_id = $wechat->id;                        $user_token->token = md5(Input::get('phone').time().rand(0,100));                        $user_token->created_at = Common::num2DateTime(time());                        $user_token->out_time_at = Common::num2DateTime(time() + Config::get("user.token_time_out"));                        $user_token->save();                        $token=$user_token->token;                    }else{                        $token=$find_token->token;                    }                }else{                    $user_token = new UserTokens;                    $user_token->user_id = $wechat->id;                    $user_token->token = md5(Input::get('phone').time().rand(0,100));                    $user_token->created_at = Common::num2DateTime(time());                    $user_token->out_time_at = Common::num2DateTime(time() + Config::get("user.token_time_out"));                    $user_token->save();                    $token=$user_token->token;                }            } catch (Exception $e){                Log::error("error", array("system" => "wechat2.0" ,"event" => "coupon_receive", 'input' =>Input::all() ,"exception" => $e));                $response = Lang::get("error.login_create_token_error_21005");                break;            }            //根据活动查到活动有效期            $activity_dateline = Coupons::getActivityBaseInfo(Config::get('c_config.ts_activity_id'));            if(!$activity_dateline){                $response = Lang::get('error.activity_not_exist');                break;            }            if(Config::get("c_config.ts_activity_open") == 1){                $coupon_res = Coupons::getNewUserRegister(Config::get('c_config.ts_activity_id'),$response);                if(is_null($coupon_res)){                    break;                }                $_coupon = array();                $_coupon_list = array();                foreach ($coupon_res as  $_c) {                    $coupon_base_info =  Coupons::getCouponBaseInfo($_c->coupon_base_info_id);                    if(!$coupon_base_info){                        $response = Lang::get('error.coupon_not_exit_error_51004');                        break;                    }                    if($coupon_base_info->status == 0){//                        $response = Lang::get('error.coupon_status_error_51005');//                        break;                        continue;                    }//                    if($coupon_base_info->date_type == 0){ //起止时间//                        if(strtotime($coupon_base_info->end_date.' 23:59:59') < strtotime(date('Y-m-d H:i:s',time()))){//                            $response = Lang::get('error.coupon_game_over_error_51006');//                            break;//                        }//                    }                    $coupons = Coupons::getReceivedCoupon($_c->coupon_base_info_id,$wechat->id);                    if($coupons){                        $response = Lang::get('error.coupon_received_error_51007');                        break;                    }                    $coupon_has_been_brought_out = Coupons::getCouponOther($_c->coupon_base_info_id);                    if(is_null($coupon_has_been_brought_out)){                        $response = Lang::get("error.coupon_has_been_brought_out_51008");                        break;                    }                    $end_date = null;                    if($coupon_base_info->date_type == 1){ //顺延日期                        $end_date = date("Y-m-d",strtotime("+".$coupon_base_info->date_num." day"));                    }                    $status = DB::update("update coupons set received_user_id =? ,get_time=?,status = 2,end_date=? where coupon_base_info_id=? and get_time ='0000-00-00 00:00:00' limit 1", array(                        $wechat->id, Common::currentTimeString(),$end_date, $_c->coupon_base_info_id                    ));                    if (!$status) {                        $response = Lang::get("error.coupon_receive_not_success_error_51009");                        break;                    }                    if($coupon_base_info->date_type == 1){                        $begin_date = date('Y-m-d',time());                        $end_date = date("Y-m-d",strtotime("+".$coupon_base_info->date_num." day"));                    }else{                        $begin_date = $coupon_base_info->begin_date;                        $end_date  = $coupon_base_info->end_date;                    }                    $_coupon['begin_date'] = $begin_date;                    $_coupon['end_date'] = $end_date;                    $_coupon['name'] = $coupon_base_info->name;                    $_coupon['limit_amount'] = $coupon_base_info->limit_amount;                    $_coupon['amount'] = $coupon_base_info->amount;                    $_coupon['type'] = Coupons::$type[$coupon_base_info->type];                    $_coupon['limit_order'] = $coupon_base_info->limit_order;                    array_push($_coupon_list, $_coupon);                }                $response["result"] = array(                    "coupon_info" => $_coupon_list,                );            }            $response["result"] = array(                "user_info" => array(                    'id'=> $wechat->id,                    'token' => $token,                    'phone' => $wechat->phone,                    'real_name' => $wechat->real_name,                    'sex' => $wechat->sex,                    'area' => $wechat->area,                    'remark' => $wechat->remark,                    'type' => $wechat->type,                    'status' =>$wechat->status,                    'nickname' => $wechat->nickname,                    'birthday' => $wechat->birthday,                    'heading_img' =>  $wechat->heading_img,                    'heading_img_url' =>  Config::get('config.heading_img_url'),                ),            );        }while(false);        Log::info("info", array("system" => "wechat2.0" ,"event" => "coupon_receive", 'input' =>Input::all() ,"output" => $response));        return json_encode($response);    }        /*     * @Author:      zy     * @DateTime:    2017-12-04 16:52:32     * @Description:沧州活动领取优惠券     * */    public function coupon_receive_cz(){        $validator = Validator::make(Input::all(),            array(                "phone" => "required",//                "activity_id" => "required|integer", //具体活动的ID                "check_code" => "required",                'register_source_id' => 'required',            )        );        $response = array('code' => 0, 'msg' => 'ok','result' => array());        do{            if($validator->fails()) {                $response = Lang::get("error.args_error_10000");                $response["result"]=$validator->messages()->all();                break;            }            $area = '010';            $record = UserCheckCode::getCheckCode(Input::get('phone'),$area);            if (is_null($record)) { //没有记录                $response = Lang::get("error.user_login_checkcode_null_10004");                break;            }            //校验是否超过时限            if (time() > Common::datetime2Num($record->out_time_at)) {                $response = Lang::get("error.user_login_checkcode_timeout_10005");                break;            }            //校验验证码是否正确            if ($record->check_code != Input::get('check_code')) {                $response = Lang::get("error.user_login_checkcode_error_10006");                break;            }            $wechat = Users::getUser(Input::get('phone'));            if (is_null($wechat)) {                try{                    $wechat = new Users();                    $wechat->phone = Input::get('phone');                    $wechat->status = 1;                    $wechat->register_source_id = Input::get('register_source_id');//1-微站 2-app 14-门店                    $wechat->real_name = '';                    $wechat->sex = 0;                    $wechat->area = '010';                    $wechat->type = 1;                    $wechat->activate = 1;                    $wechat->nickname = '';                    $wechat->heading_img = '';                    $wechat->member = Config::get('user.def_member');                    $wechat->save();                    //新用户注册用券                    if(Config::get("config.register_coupons") == 1){                        $activity = Coupons::getNewUserRegister(Config::get('config.activity_id'),$response);                        if(!is_null($activity)){                            foreach ($activity as  $v) {                                $coupon_base_info = Coupons::getCouponBaseInfo($v->coupon_base_info_id);                                if (!$coupon_base_info) {                                    continue;                                }                                if ($coupon_base_info->status == 0) {                                    continue;                                }                                if ($coupon_base_info->date_type == 0 && strtotime($coupon_base_info->end_date.' 23:59:59') < strtotime(date('Y-m-d H:i:s', time()))) { //起止时间                                    continue;                                }                                $coupon_whether_exist = Coupons::getCouponOther($v->coupon_base_info_id);                                if (!is_null($coupon_whether_exist)) {                                    $coupons = Coupons::find($coupon_whether_exist->id);                                    $coupons->status = 2;                                    $coupons->received_user_id = $wechat->id;                                    $coupons->get_time = date("Y-m-d H:i:s", time());                                    if ($coupon_base_info->date_type == 1) { //顺延日期                                        $coupons->end_date = date("Y-m-d", strtotime("+" . $coupon_base_info->date_num . " day"));                                    }                                    $coupons->save();                                }                            }                        }                    }                }catch (Exception $e){                    Log::error("error", array("system" => "wechat2.0" ,"event" => "coupon_receive_cz", 'input' =>Input::all() ,"exception" => $e));                    $response = Lang::get("error.login_create_token_error_21005");                    break;                }            }            if($wechat->status == 2){                $response = Lang::get('error.user_frozen_error_21002');                break;            }            //如果发现此用户是老用户，且未激活 直接激活            if($wechat->type == 1 && $wechat->activate == 0){                //注册送券                if(Config::get("config.register_coupons") == 1){                    $activity = Coupons::getNewUserRegister(Config::get('config.activity_id'),$response);                    if(!is_null($activity)){                        foreach ($activity as  $v) {                            $coupon_base_info = Coupons::getCouponBaseInfo($v->coupon_base_info_id);                            if (!$coupon_base_info) {                                continue;                            }                            if ($coupon_base_info->status == 0) {                                continue;                            }                            if ($coupon_base_info->date_type == 0 && strtotime($coupon_base_info->end_date.' 23:59:59') < strtotime(date('Y-m-d H:i:s', time()))) { //起止时间                                continue;                            }                            $coupon_whether_exist = Coupons::getCouponOther($v->coupon_base_info_id);                            if (!is_null($coupon_whether_exist)) {                                $coupons = Coupons::find($coupon_whether_exist->id);                                $coupons->status = 2;                                $coupons->received_user_id = $wechat->id;                                $coupons->get_time = date("Y-m-d H:i:s", time());                                if ($coupon_base_info->date_type == 1) { //顺延日期                                    $coupons->end_date = date("Y-m-d", strtotime("+" . $coupon_base_info->date_num . " day"));                                }                                $coupons->save();                            }                        }                    }                }                $wechat->activate = 1;                $wechat->save();            }            try{                $token="";                $find_token = UserTokens::where('user_id',$wechat->id)->first();                if(!is_null($find_token)){ //有token 判断token是否过期                    if(strtotime($find_token->out_time_at) < time()){//说明过期了                        UserTokens::where('user_id',$wechat->id)->delete();                        $user_token = new UserTokens;                        $user_token->user_id = $wechat->id;                        $user_token->token = md5(Input::get('phone').time().rand(0,100));                        $user_token->created_at = Common::num2DateTime(time());                        $user_token->out_time_at = Common::num2DateTime(time() + Config::get("user.token_time_out"));                        $user_token->save();                        $token=$user_token->token;                    }else{                        $token=$find_token->token;                    }                }else{                    $user_token = new UserTokens;                    $user_token->user_id = $wechat->id;                    $user_token->token = md5(Input::get('phone').time().rand(0,100));                    $user_token->created_at = Common::num2DateTime(time());                    $user_token->out_time_at = Common::num2DateTime(time() + Config::get("user.token_time_out"));                    $user_token->save();                    $token=$user_token->token;                }            } catch (Exception $e){                Log::error("error", array("system" => "wechat2.0" ,"event" => "coupon_receive_cz", 'input' =>Input::all() ,"exception" => $e));                $response = Lang::get("error.login_create_token_error_21005");                break;            }            //根据活动查到活动有效期            $activity_dateline = Coupons::getActivityBaseInfo(Config::get('c_config.cz_activity_id'));            if(!$activity_dateline){                $response = Lang::get('error.activity_not_exist');                break;            }            if(Config::get("c_config.cz_activity_open") == 1){                $coupon_res = Coupons::getNewUserRegister(Config::get('c_config.cz_activity_id'),$response);                if(is_null($coupon_res)){                    break;                }                $_coupon = array();                $_coupon_list = array();                foreach ($coupon_res as  $_c) {                    $coupon_base_info =  Coupons::getCouponBaseInfo($_c->coupon_base_info_id);                    if(!$coupon_base_info){                        $response = Lang::get('error.coupon_not_exit_error_51004');                        break;                    }                    if($coupon_base_info->status == 0){//                        $response = Lang::get('error.coupon_status_error_51005');//                        break;                        continue;                    }//                    if($coupon_base_info->date_type == 0){ //起止时间//                        if(strtotime($coupon_base_info->end_date.' 23:59:59') < strtotime(date('Y-m-d H:i:s',time()))){//                            $response = Lang::get('error.coupon_game_over_error_51006');//                            break;//                        }//                    }                    $coupons = Coupons::getReceivedCoupon($_c->coupon_base_info_id,$wechat->id);                    if($coupons){                        $response = Lang::get('error.coupon_received_error_51007');                        break;                    }                    $coupon_has_been_brought_out = Coupons::getCouponOther($_c->coupon_base_info_id);                    if(is_null($coupon_has_been_brought_out)){                        $response = Lang::get("error.coupon_has_been_brought_out_51008");                        break;                    }                    $end_date = null;                    if($coupon_base_info->date_type == 1){ //顺延日期                        $end_date = date("Y-m-d",strtotime("+".$coupon_base_info->date_num." day"));                    }                    $status = DB::update("update coupons set received_user_id =? ,get_time=?,status = 2,end_date=? where coupon_base_info_id=? and get_time ='0000-00-00 00:00:00' limit 1", array(                        $wechat->id, Common::currentTimeString(),$end_date, $_c->coupon_base_info_id                    ));                    if (!$status) {                        $response = Lang::get("error.coupon_receive_not_success_error_51009");                        break;                    }                    if($coupon_base_info->date_type == 1){                        $begin_date = date('Y-m-d',time());                        $end_date = date("Y-m-d",strtotime("+".$coupon_base_info->date_num." day"));                    }else{                        $begin_date = $coupon_base_info->begin_date;                        $end_date  = $coupon_base_info->end_date;                    }                    $_coupon['begin_date'] = $begin_date;                    $_coupon['end_date'] = $end_date;                    $_coupon['name'] = $coupon_base_info->name;                    $_coupon['limit_amount'] = $coupon_base_info->limit_amount;                    $_coupon['amount'] = $coupon_base_info->amount;                    $_coupon['type'] = Coupons::$type[$coupon_base_info->type];                    $_coupon['limit_order'] = $coupon_base_info->limit_order;                    array_push($_coupon_list, $_coupon);                }                $response["result"] = array(                    "coupon_info" => $_coupon_list,                );            }            $response["result"] = array(                "user_info" => array(                    'id'=> $wechat->id,                    'token' => $token,                    'phone' => $wechat->phone,                    'real_name' => $wechat->real_name,                    'sex' => $wechat->sex,                    'area' => $wechat->area,                    'remark' => $wechat->remark,                    'type' => $wechat->type,                    'status' =>$wechat->status,                    'nickname' => $wechat->nickname,                    'birthday' => $wechat->birthday,                    'heading_img' =>  $wechat->heading_img,                    'heading_img_url' =>  Config::get('config.heading_img_url'),                ),            );        }while(false);        Log::info("info", array("system" => "wechat2.0" ,"event" => "coupon_receive_cz", 'input' =>Input::all() ,"output" => $response));        return json_encode($response);    }        /*     * @Author:      zy     * @DateTime:    2017-12-04 16:52:32     * @Description:沧州活动领取优惠券     * */    public function coupon_receive_cz2(){        $validator = Validator::make(Input::all(),            array(                "phone" => "required",//                "activity_id" => "required|integer", //具体活动的ID                "check_code" => "required",                'register_source_id' => 'required',            )        );        $response = array('code' => 0, 'msg' => 'ok','result' => array());        do{            if($validator->fails()) {                $response = Lang::get("error.args_error_10000");                $response["result"]=$validator->messages()->all();                break;            }            $area = '010';            $record = UserCheckCode::getCheckCode(Input::get('phone'),$area);            if (is_null($record)) { //没有记录                $response = Lang::get("error.user_login_checkcode_null_10004");                break;            }            //校验是否超过时限            if (time() > Common::datetime2Num($record->out_time_at)) {                $response = Lang::get("error.user_login_checkcode_timeout_10005");                break;            }            //校验验证码是否正确            if ($record->check_code != Input::get('check_code')) {                $response = Lang::get("error.user_login_checkcode_error_10006");                break;            }            $wechat = Users::getUser(Input::get('phone'));            if (is_null($wechat)) {                try{                    $wechat = new Users();                    $wechat->phone = Input::get('phone');                    $wechat->status = 1;                    $wechat->register_source_id = Input::get('register_source_id');//1-微站 2-app 14-门店                    $wechat->real_name = '';                    $wechat->sex = 0;                    $wechat->area = '010';                    $wechat->type = 1;                    $wechat->activate = 1;                    $wechat->nickname = '';                    $wechat->heading_img = '';                    $wechat->member = Config::get('user.def_member');                    $wechat->save();                    //新用户注册用券                    if(Config::get("config.register_coupons") == 1){                        $activity = Coupons::getNewUserRegister(Config::get('config.activity_id'),$response);                        if(!is_null($activity)){                            foreach ($activity as  $v) {                                $coupon_base_info = Coupons::getCouponBaseInfo($v->coupon_base_info_id);                                if (!$coupon_base_info) {                                    continue;                                }                                if ($coupon_base_info->status == 0) {                                    continue;                                }                                if ($coupon_base_info->date_type == 0 && strtotime($coupon_base_info->end_date.' 23:59:59') < strtotime(date('Y-m-d H:i:s', time()))) { //起止时间                                    continue;                                }                                $coupon_whether_exist = Coupons::getCouponOther($v->coupon_base_info_id);                                if (!is_null($coupon_whether_exist)) {                                    $coupons = Coupons::find($coupon_whether_exist->id);                                    $coupons->status = 2;                                    $coupons->received_user_id = $wechat->id;                                    $coupons->get_time = date("Y-m-d H:i:s", time());                                    if ($coupon_base_info->date_type == 1) { //顺延日期                                        $coupons->end_date = date("Y-m-d", strtotime("+" . $coupon_base_info->date_num . " day"));                                    }                                    $coupons->save();                                }                            }                        }                    }                }catch (Exception $e){                    Log::error("error", array("system" => "wechat2.0" ,"event" => "coupon_receive_cz2", 'input' =>Input::all() ,"exception" => $e));                    $response = Lang::get("error.login_create_token_error_21005");                    break;                }            }            if($wechat->status == 2){                $response = Lang::get('error.user_frozen_error_21002');                break;            }            //如果发现此用户是老用户，且未激活 直接激活            if($wechat->type == 1 && $wechat->activate == 0){                //注册送券                if(Config::get("config.register_coupons") == 1){                    $activity = Coupons::getNewUserRegister(Config::get('config.activity_id'),$response);                    if(!is_null($activity)){                        foreach ($activity as  $v) {                            $coupon_base_info = Coupons::getCouponBaseInfo($v->coupon_base_info_id);                            if (!$coupon_base_info) {                                continue;                            }                            if ($coupon_base_info->status == 0) {                                continue;                            }                            if ($coupon_base_info->date_type == 0 && strtotime($coupon_base_info->end_date.' 23:59:59') < strtotime(date('Y-m-d H:i:s', time()))) { //起止时间                                continue;                            }                            $coupon_whether_exist = Coupons::getCouponOther($v->coupon_base_info_id);                            if (!is_null($coupon_whether_exist)) {                                $coupons = Coupons::find($coupon_whether_exist->id);                                $coupons->status = 2;                                $coupons->received_user_id = $wechat->id;                                $coupons->get_time = date("Y-m-d H:i:s", time());                                if ($coupon_base_info->date_type == 1) { //顺延日期                                    $coupons->end_date = date("Y-m-d", strtotime("+" . $coupon_base_info->date_num . " day"));                                }                                $coupons->save();                            }                        }                    }                }                $wechat->activate = 1;                $wechat->save();            }            try{                $token="";                $find_token = UserTokens::where('user_id',$wechat->id)->first();                if(!is_null($find_token)){ //有token 判断token是否过期                    if(strtotime($find_token->out_time_at) < time()){//说明过期了                        UserTokens::where('user_id',$wechat->id)->delete();                        $user_token = new UserTokens;                        $user_token->user_id = $wechat->id;                        $user_token->token = md5(Input::get('phone').time().rand(0,100));                        $user_token->created_at = Common::num2DateTime(time());                        $user_token->out_time_at = Common::num2DateTime(time() + Config::get("user.token_time_out"));                        $user_token->save();                        $token=$user_token->token;                    }else{                        $token=$find_token->token;                    }                }else{                    $user_token = new UserTokens;                    $user_token->user_id = $wechat->id;                    $user_token->token = md5(Input::get('phone').time().rand(0,100));                    $user_token->created_at = Common::num2DateTime(time());                    $user_token->out_time_at = Common::num2DateTime(time() + Config::get("user.token_time_out"));                    $user_token->save();                    $token=$user_token->token;                }            } catch (Exception $e){                Log::error("error", array("system" => "wechat2.0" ,"event" => "coupon_receive_cz2", 'input' =>Input::all() ,"exception" => $e));                $response = Lang::get("error.login_create_token_error_21005");                break;            }            //根据活动查到活动有效期            $activity_dateline = Coupons::getActivityBaseInfo(Config::get('c_config.cz2_activity_id'));            if(!$activity_dateline){                $response = Lang::get('error.activity_not_exist');                break;            }            if(Config::get("c_config.cz_activity_open") == 1){                $coupon_res = Coupons::getNewUserRegister(Config::get('c_config.cz2_activity_id'),$response);                if(is_null($coupon_res)){                    break;                }                $_coupon = array();                $_coupon_list = array();                foreach ($coupon_res as  $_c) {                    $coupon_base_info =  Coupons::getCouponBaseInfo($_c->coupon_base_info_id);                    if(!$coupon_base_info){                        $response = Lang::get('error.coupon_not_exit_error_51004');                        break;                    }                    if($coupon_base_info->status == 0){//                        $response = Lang::get('error.coupon_status_error_51005');//                        break;                        continue;                    }//                    if($coupon_base_info->date_type == 0){ //起止时间//                        if(strtotime($coupon_base_info->end_date.' 23:59:59') < strtotime(date('Y-m-d H:i:s',time()))){//                            $response = Lang::get('error.coupon_game_over_error_51006');//                            break;//                        }//                    }                    $coupons = Coupons::getReceivedCoupon($_c->coupon_base_info_id,$wechat->id);                    if($coupons){                        $response = Lang::get('error.coupon_received_error_51007');                        break;                    }                    $coupon_has_been_brought_out = Coupons::getCouponOther($_c->coupon_base_info_id);                    if(is_null($coupon_has_been_brought_out)){                        $response = Lang::get("error.coupon_has_been_brought_out_51008");                        break;                    }                    $end_date = null;                    if($coupon_base_info->date_type == 1){ //顺延日期                        $end_date = date("Y-m-d",strtotime("+".$coupon_base_info->date_num." day"));                    }                    $status = DB::update("update coupons set received_user_id =? ,get_time=?,status = 2,end_date=? where coupon_base_info_id=? and get_time ='0000-00-00 00:00:00' limit 1", array(                        $wechat->id, Common::currentTimeString(),$end_date, $_c->coupon_base_info_id                    ));                    if (!$status) {                        $response = Lang::get("error.coupon_receive_not_success_error_51009");                        break;                    }                    if($coupon_base_info->date_type == 1){                        $begin_date = date('Y-m-d',time());                        $end_date = date("Y-m-d",strtotime("+".$coupon_base_info->date_num." day"));                    }else{                        $begin_date = $coupon_base_info->begin_date;                        $end_date  = $coupon_base_info->end_date;                    }                    $_coupon['begin_date'] = $begin_date;                    $_coupon['end_date'] = $end_date;                    $_coupon['name'] = $coupon_base_info->name;                    $_coupon['limit_amount'] = $coupon_base_info->limit_amount;                    $_coupon['amount'] = $coupon_base_info->amount;                    $_coupon['type'] = Coupons::$type[$coupon_base_info->type];                    $_coupon['limit_order'] = $coupon_base_info->limit_order;                    array_push($_coupon_list, $_coupon);                }                $response["result"] = array(                    "coupon_info" => $_coupon_list,                );            }            $response["result"] = array(                "user_info" => array(                    'id'=> $wechat->id,                    'token' => $token,                    'phone' => $wechat->phone,                    'real_name' => $wechat->real_name,                    'sex' => $wechat->sex,                    'area' => $wechat->area,                    'remark' => $wechat->remark,                    'type' => $wechat->type,                    'status' =>$wechat->status,                    'nickname' => $wechat->nickname,                    'birthday' => $wechat->birthday,                    'heading_img' =>  $wechat->heading_img,                    'heading_img_url' =>  Config::get('config.heading_img_url'),                ),            );        }while(false);        Log::info("info", array("system" => "wechat2.0" ,"event" => "coupon_receive_cz", 'input' =>Input::all() ,"output" => $response));        return json_encode($response);    }    /**     * @Author   lyt                      <ahlyt@anhuibj.com>     * @DateTime 2018-03-15T10:18:47+0800     * @return   [type]                   [description]     * 3月19 北京洗车活动     */    public function coupon_receive_bj(){        $validator = Validator::make(Input::all(),            array(                "phone" => "required",               // "activity_id" => "required|integer", //具体活动的ID                "check_code" => "required",                'register_source_id' => 'required',            )        );        $response = array('code' => 0, 'msg' => 'ok','result' => array());        do{            if($validator->fails()) {                $response = Lang::get("error.args_error_10000");                $response["result"]=$validator->messages()->all();                break;            }            $area = '010';            $record = UserCheckCode::getCheckCode(Input::get('phone'),$area);            if (is_null($record)) { //没有记录                $response = Lang::get("error.user_login_checkcode_null_10004");                break;            }            //校验是否超过时限            if (time() > Common::datetime2Num($record->out_time_at)) {                $response = Lang::get("error.user_login_checkcode_timeout_10005");                break;            }            //校验验证码是否正确            if ($record->check_code != Input::get('check_code')) {                $response = Lang::get("error.user_login_checkcode_error_10006");                break;            }            $wechat = Users::getUser(Input::get('phone'));            if (is_null($wechat)) {                try{                    $wechat = new Users();                    $wechat->phone = Input::get('phone');                    $wechat->status = 1;                    $wechat->register_source_id = Input::get('register_source_id');//1-微站 2-app 14-门店                    $wechat->real_name = '';                    $wechat->sex = 0;                    $wechat->area = '010';                    $wechat->type = 1;                    $wechat->activate = 1;                    $wechat->nickname = '';                    $wechat->heading_img = '';                    $wechat->save();                    //新用户注册用券                    if(Config::get("config.register_coupons") == 1){                        $activity = Coupons::getNewUserRegister(Config::get('config.activity_id'),$response);                        if(!is_null($activity)){                            foreach ($activity as  $v) {                                $coupon_base_info = Coupons::getCouponBaseInfo($v->coupon_base_info_id);                                if (!$coupon_base_info) {                                    continue;                                }                                if ($coupon_base_info->status == 0) {                                    continue;                                }                                if ($coupon_base_info->date_type == 0 && strtotime($coupon_base_info->end_date.' 23:59:59') < strtotime(date('Y-m-d H:i:s', time()))) { //起止时间                                    continue;                                }                                $coupon_whether_exist = Coupons::getCouponOther($v->coupon_base_info_id);                                if (!is_null($coupon_whether_exist)) {                                    $coupons = Coupons::find($coupon_whether_exist->id);                                    $coupons->status = 2;                                    $coupons->received_user_id = $wechat->id;                                    $coupons->get_time = date("Y-m-d H:i:s", time());                                    if ($coupon_base_info->date_type == 1) { //顺延日期                                        $coupons->end_date = date("Y-m-d", strtotime("+" . $coupon_base_info->date_num . " day"));                                    }                                    $coupons->save();                                }                            }                        }                    }                }catch (Exception $e){                    Log::error("error", array("system" => "wechat2.0" ,"event" => "coupon_receive_bj", 'input' =>Input::all() ,"exception" => $e));                    $response = Lang::get("error.login_create_token_error_21005");                    break;                }            }else{                $response = Lang::get('error.user_already_existed_21008');                break;            }            // if($wechat->status == 2){            //     $response = Lang::get('error.user_frozen_error_21002');            //     break;            // }            //如果发现此用户是老用户，且未激活 直接激活            // if($wechat->type == 1 && $wechat->activate == 0){            //     //注册送券            //     if(Config::get("config.register_coupons") == 1){            //         $activity = Coupons::getNewUserRegister(Config::get('config.activity_id'),$response);            //         if(!is_null($activity)){            //             foreach ($activity as  $v) {            //                 $coupon_base_info = Coupons::getCouponBaseInfo($v->coupon_base_info_id);            //                 if (!$coupon_base_info) {            //                     continue;            //                 }            //                 if ($coupon_base_info->status == 0) {            //                     continue;            //                 }            //                 if ($coupon_base_info->date_type == 0 && strtotime($coupon_base_info->end_date.' 23:59:59') < strtotime(date('Y-m-d H:i:s', time()))) { //起止时间            //                     continue;            //                 }            //                 $coupon_whether_exist = Coupons::getCouponOther($v->coupon_base_info_id);            //                 if (!is_null($coupon_whether_exist)) {            //                     $coupons = Coupons::find($coupon_whether_exist->id);            //                     $coupons->status = 2;            //                     $coupons->received_user_id = $wechat->id;            //                     $coupons->get_time = date("Y-m-d H:i:s", time());            //                     if ($coupon_base_info->date_type == 1) { //顺延日期            //                         $coupons->end_date = date("Y-m-d", strtotime("+" . $coupon_base_info->date_num . " day"));            //                     }            //                     $coupons->save();            //                 }            //             }            //         }            //     }            //     $wechat->activate = 1;            //     $wechat->save();            // }            try{                $token="";                $find_token = UserTokens::where('user_id',$wechat->id)->first();                if(!is_null($find_token)){ //有token 判断token是否过期                    if(strtotime($find_token->out_time_at) < time()){//说明过期了                        UserTokens::where('user_id',$wechat->id)->delete();                        $user_token = new UserTokens;                        $user_token->user_id = $wechat->id;                        $user_token->token = md5(Input::get('phone').time().rand(0,100));                        $user_token->created_at = Common::num2DateTime(time());                        $user_token->out_time_at = Common::num2DateTime(time() + Config::get("user.token_time_out"));                        $user_token->save();                        $token=$user_token->token;                    }else{                        $token=$find_token->token;                    }                }else{                    $user_token = new UserTokens;                    $user_token->user_id = $wechat->id;                    $user_token->token = md5(Input::get('phone').time().rand(0,100));                    $user_token->created_at = Common::num2DateTime(time());                    $user_token->out_time_at = Common::num2DateTime(time() + Config::get("user.token_time_out"));                    $user_token->save();                    $token=$user_token->token;                }            } catch (Exception $e){                Log::error("error", array("system" => "wechat2.0" ,"event" => "coupon_receive_bj", 'input' =>Input::all() ,"exception" => $e));                $response = Lang::get("error.login_create_token_error_21005");                break;            }            //根据活动查到活动有效期            $activity_dateline = Coupons::getActivityBaseInfo(Config::get('c_config.bj_activity_id'));            if(!$activity_dateline){                $response = Lang::get('error.activity_not_exist');                break;            }            if(Config::get("c_config.bj_activity_open") == 1){                $coupon_res = Coupons::getNewUserRegister(Config::get('c_config.bj_activity_id'),$response);                if(is_null($coupon_res)){                    break;                }                $_coupon = array();                $_coupon_list = array();                foreach ($coupon_res as  $_c) {                    $coupon_base_info =  Coupons::getCouponBaseInfo($_c->coupon_base_info_id);                    if(!$coupon_base_info){                        $response = Lang::get('error.coupon_not_exit_error_51004');                        break;                    }                    if($coupon_base_info->status == 0){                       // $response = Lang::get('error.coupon_status_error_51005');                       // break;                        continue;                    }                   // if($coupon_base_info->date_type == 0){ //起止时间                   //     if(strtotime($coupon_base_info->end_date.' 23:59:59') < strtotime(date('Y-m-d H:i:s',time()))){                   //         $response = Lang::get('error.coupon_game_over_error_51006');                   //         break;                   //     }                   // }                    $coupons = Coupons::getReceivedCoupon($_c->coupon_base_info_id,$wechat->id);                    if($coupons){                        $response = Lang::get('error.coupon_received_error_51007');                        break;                    }                    $coupon_has_been_brought_out = Coupons::getCouponOther($_c->coupon_base_info_id);                    if(is_null($coupon_has_been_brought_out)){                        $response = Lang::get("error.coupon_has_been_brought_out_51008");                        break;                    }                    $end_date = null;                    if($coupon_base_info->date_type == 1){ //顺延日期                        $end_date = date("Y-m-d",strtotime("+".$coupon_base_info->date_num." day"));                    }                    $status = DB::update("update coupons set received_user_id =? ,get_time=?,status = 2,end_date=? where coupon_base_info_id=? and get_time ='0000-00-00 00:00:00' limit 1", array(                        $wechat->id, Common::currentTimeString(),$end_date, $_c->coupon_base_info_id                    ));                    if (!$status) {                        $response = Lang::get("error.coupon_receive_not_success_error_51009");                        break;                    }                    if($coupon_base_info->date_type == 1){                        $begin_date = date('Y-m-d',time());                        $end_date = date("Y-m-d",strtotime("+".$coupon_base_info->date_num." day"));                    }else{                        $begin_date = $coupon_base_info->begin_date;                        $end_date  = $coupon_base_info->end_date;                    }                    $_coupon['begin_date'] = $begin_date;                    $_coupon['end_date'] = $end_date;                    $_coupon['name'] = $coupon_base_info->name;                    $_coupon['limit_amount'] = $coupon_base_info->limit_amount;                    $_coupon['amount'] = $coupon_base_info->amount;                    $_coupon['type'] = Coupons::$type[$coupon_base_info->type];                    $_coupon['limit_order'] = $coupon_base_info->limit_order;                    array_push($_coupon_list, $_coupon);                }                $response["result"] = array(                    "coupon_info" => $_coupon_list,                );            }            $response["result"] = array(                "user_info" => array(                    'id'=> $wechat->id,                    'token' => $token,                    'phone' => $wechat->phone,                    'real_name' => $wechat->real_name,                    'sex' => $wechat->sex,                    'area' => $wechat->area,                    'remark' => $wechat->remark,                    'type' => $wechat->type,                    'status' =>$wechat->status,                    'nickname' => $wechat->nickname,                    'birthday' => $wechat->birthday,                    'heading_img' =>  $wechat->heading_img,                    'heading_img_url' =>  Config::get('config.heading_img_url'),                ),            );        }while(false);        Log::info("info", array("system" => "wechat2.0" ,"event" => "coupon_receive_bj", 'input' =>Input::all() ,"output" => $response));        return json_encode($response);    }    /**     * @Author   lyt                      <ahlyt@anhuibj.com>     * @DateTime 2018-03-15T10:32:20+0800     * @return   [type]                   [description]     * 3月19 河北赤峰洗车活动     */    public function coupon_receive_hecf(){        $validator = Validator::make(Input::all(),            array(                "phone" => "required",                //"activity_id" => "required|integer", //具体活动的ID                "check_code" => "required",                'register_source_id' => 'required',            )        );        $response = array('code' => 0, 'msg' => 'ok','result' => array());        do{            if($validator->fails()) {                $response = Lang::get("error.args_error_10000");                $response["result"]=$validator->messages()->all();                break;            }            $area = '010';            $record = UserCheckCode::getCheckCode(Input::get('phone'),$area);            if (is_null($record)) { //没有记录                $response = Lang::get("error.user_login_checkcode_null_10004");                break;            }            //校验是否超过时限            if (time() > Common::datetime2Num($record->out_time_at)) {                $response = Lang::get("error.user_login_checkcode_timeout_10005");                break;            }            //校验验证码是否正确            if ($record->check_code != Input::get('check_code')) {                $response = Lang::get("error.user_login_checkcode_error_10006");                break;            }            $wechat = Users::getUser(Input::get('phone'));            if (is_null($wechat)) {                try{                    $wechat = new Users();                    $wechat->phone = Input::get('phone');                    $wechat->status = 1;                    $wechat->register_source_id = Input::get('register_source_id');//1-微站 2-app 14-门店                    $wechat->real_name = '';                    $wechat->sex = 0;                    $wechat->area = '010';                    $wechat->type = 1;                    $wechat->activate = 1;                    $wechat->nickname = '';                    $wechat->heading_img = '';                    $wechat->member = Config::get('user.def_member');                    $wechat->save();                    //新用户注册用券                    if(Config::get("config.register_coupons") == 1){                        $activity = Coupons::getNewUserRegister(Config::get('config.activity_id'),$response);                        if(!is_null($activity)){                            foreach ($activity as  $v) {                                $coupon_base_info = Coupons::getCouponBaseInfo($v->coupon_base_info_id);                                if (!$coupon_base_info) {                                    continue;                                }                                if ($coupon_base_info->status == 0) {                                    continue;                                }                                if ($coupon_base_info->date_type == 0 && strtotime($coupon_base_info->end_date.' 23:59:59') < strtotime(date('Y-m-d H:i:s', time()))) { //起止时间                                    continue;                                }                                $coupon_whether_exist = Coupons::getCouponOther($v->coupon_base_info_id);                                if (!is_null($coupon_whether_exist)) {                                    $coupons = Coupons::find($coupon_whether_exist->id);                                    $coupons->status = 2;                                    $coupons->received_user_id = $wechat->id;                                    $coupons->get_time = date("Y-m-d H:i:s", time());                                    if ($coupon_base_info->date_type == 1) { //顺延日期                                        $coupons->end_date = date("Y-m-d", strtotime("+" . $coupon_base_info->date_num . " day"));                                    }                                    $coupons->save();                                }                            }                        }                    }                }catch (Exception $e){                    Log::error("error", array("system" => "wechat2.0" ,"event" => "coupon_receive_bj", 'input' =>Input::all() ,"exception" => $e));                    $response = Lang::get("error.login_create_token_error_21005");                    break;                }            }else{                $response = Lang::get('error.user_already_existed_21008');                break;            }            // if($wechat->status == 2){            //     $response = Lang::get('error.user_frozen_error_21002');            //     break;            // }            //如果发现此用户是老用户，且未激活 直接激活            // if($wechat->type == 1 && $wechat->activate == 0){            //     //注册送券            //     if(Config::get("config.register_coupons") == 1){            //         $activity = Coupons::getNewUserRegister(Config::get('config.activity_id'),$response);            //         if(!is_null($activity)){            //             foreach ($activity as  $v) {            //                 $coupon_base_info = Coupons::getCouponBaseInfo($v->coupon_base_info_id);            //                 if (!$coupon_base_info) {            //                     continue;            //                 }            //                 if ($coupon_base_info->status == 0) {            //                     continue;            //                 }            //                 if ($coupon_base_info->date_type == 0 && strtotime($coupon_base_info->end_date.' 23:59:59') < strtotime(date('Y-m-d H:i:s', time()))) { //起止时间            //                     continue;            //                 }            //                 $coupon_whether_exist = Coupons::getCouponOther($v->coupon_base_info_id);            //                 if (!is_null($coupon_whether_exist)) {            //                     $coupons = Coupons::find($coupon_whether_exist->id);            //                     $coupons->status = 2;            //                     $coupons->received_user_id = $wechat->id;            //                     $coupons->get_time = date("Y-m-d H:i:s", time());            //                     if ($coupon_base_info->date_type == 1) { //顺延日期            //                         $coupons->end_date = date("Y-m-d", strtotime("+" . $coupon_base_info->date_num . " day"));            //                     }            //                     $coupons->save();            //                 }            //             }            //         }            //     }            //     $wechat->activate = 1;            //     $wechat->save();            // }            try{                $token="";                $find_token = UserTokens::where('user_id',$wechat->id)->first();                if(!is_null($find_token)){ //有token 判断token是否过期                    if(strtotime($find_token->out_time_at) < time()){//说明过期了                        UserTokens::where('user_id',$wechat->id)->delete();                        $user_token = new UserTokens;                        $user_token->user_id = $wechat->id;                        $user_token->token = md5(Input::get('phone').time().rand(0,100));                        $user_token->created_at = Common::num2DateTime(time());                        $user_token->out_time_at = Common::num2DateTime(time() + Config::get("user.token_time_out"));                        $user_token->save();                        $token=$user_token->token;                    }else{                        $token=$find_token->token;                    }                }else{                    $user_token = new UserTokens;                    $user_token->user_id = $wechat->id;                    $user_token->token = md5(Input::get('phone').time().rand(0,100));                    $user_token->created_at = Common::num2DateTime(time());                    $user_token->out_time_at = Common::num2DateTime(time() + Config::get("user.token_time_out"));                    $user_token->save();                    $token=$user_token->token;                }            } catch (Exception $e){                Log::error("error", array("system" => "wechat2.0" ,"event" => "coupon_receive_hbcf", 'input' =>Input::all() ,"exception" => $e));                $response = Lang::get("error.login_create_token_error_21005");                break;            }            //根据活动查到活动有效期            $activity_dateline = Coupons::getActivityBaseInfo(Config::get('c_config.hbcf_activity_id'));            if(!$activity_dateline){                $response = Lang::get('error.activity_not_exist');                break;            }            if(Config::get("c_config.hbcf_activity_open") == 1){                $coupon_res = Coupons::getNewUserRegister(Config::get('c_config.hbcf_activity_id'),$response);                if(is_null($coupon_res)){                    break;                }                $_coupon = array();                $_coupon_list = array();                foreach ($coupon_res as  $_c) {                    $coupon_base_info =  Coupons::getCouponBaseInfo($_c->coupon_base_info_id);                    if(!$coupon_base_info){                        $response = Lang::get('error.coupon_not_exit_error_51004');                        break;                    }                    if($coupon_base_info->status == 0){                       // $response = Lang::get('error.coupon_status_error_51005');                       // break;                        continue;                    }                   // if($coupon_base_info->date_type == 0){ //起止时间                   //     if(strtotime($coupon_base_info->end_date.' 23:59:59') < strtotime(date('Y-m-d H:i:s',time()))){                   //         $response = Lang::get('error.coupon_game_over_error_51006');                   //         break;                   //     }                   // }                    $coupons = Coupons::getReceivedCoupon($_c->coupon_base_info_id,$wechat->id);                    if($coupons){                        $response = Lang::get('error.coupon_received_error_51007');                        break;                    }                    $coupon_has_been_brought_out = Coupons::getCouponOther($_c->coupon_base_info_id);                    if(is_null($coupon_has_been_brought_out)){                        $response = Lang::get("error.coupon_has_been_brought_out_51008");                        break;                    }                    $end_date = null;                    if($coupon_base_info->date_type == 1){ //顺延日期                        $end_date = date("Y-m-d",strtotime("+".$coupon_base_info->date_num." day"));                    }                    $status = DB::update("update coupons set received_user_id =? ,get_time=?,status = 2,end_date=? where coupon_base_info_id=? and get_time ='0000-00-00 00:00:00' limit 1", array(                        $wechat->id, Common::currentTimeString(),$end_date, $_c->coupon_base_info_id                    ));                    if (!$status) {                        $response = Lang::get("error.coupon_receive_not_success_error_51009");                        break;                    }                    if($coupon_base_info->date_type == 1){                        $begin_date = date('Y-m-d',time());                        $end_date = date("Y-m-d",strtotime("+".$coupon_base_info->date_num." day"));                    }else{                        $begin_date = $coupon_base_info->begin_date;                        $end_date  = $coupon_base_info->end_date;                    }                    $_coupon['begin_date'] = $begin_date;                    $_coupon['end_date'] = $end_date;                    $_coupon['name'] = $coupon_base_info->name;                    $_coupon['limit_amount'] = $coupon_base_info->limit_amount;                    $_coupon['amount'] = $coupon_base_info->amount;                    $_coupon['type'] = Coupons::$type[$coupon_base_info->type];                    $_coupon['limit_order'] = $coupon_base_info->limit_order;                    array_push($_coupon_list, $_coupon);                }                $response["result"] = array(                    "coupon_info" => $_coupon_list,                );            }            $response["result"] = array(                "user_info" => array(                    'id'=> $wechat->id,                    'token' => $token,                    'phone' => $wechat->phone,                    'real_name' => $wechat->real_name,                    'sex' => $wechat->sex,                    'area' => $wechat->area,                    'remark' => $wechat->remark,                    'type' => $wechat->type,                    'status' =>$wechat->status,                    'nickname' => $wechat->nickname,                    'birthday' => $wechat->birthday,                    'heading_img' =>  $wechat->heading_img,                    'heading_img_url' =>  Config::get('config.heading_img_url'),                ),            );        }while(false);        Log::info("info", array("system" => "wechat2.0" ,"event" => "coupon_receive_hbcf", 'input' =>Input::all() ,"output" => $response));        return json_encode($response);    }            /* * @Author:      kc * @DateTime:    2017-08-23 10:51:20 * @Description: 邀请注册活动 * */    public function inviteRegister(){        $validator = Validator::make(Input::all(),            array(                'phone' => 'required',                'check_code' => 'required',                'invite_id' => 'required',            )        );        do {            if ($validator->fails()) {                $response = Lang::get("error.args_error_10000");                $response["result"] = $validator->messages()->all();                break;            }            $area = '010';            $record = UserCheckCode::getCheckCode(Input::get('phone'), $area);            if (is_null($record)) { //没有记录                $response = Lang::get("error.user_login_checkcode_null_10004");                break;            }            //校验是否超过时限            if (time() > Common::datetime2Num($record->out_time_at)) {                $response = Lang::get("error.user_login_checkcode_timeout_10005");                break;            }            //校验验证码是否正确            if ($record->check_code != Input::get('check_code')) {                $response = Lang::get("error.user_login_checkcode_error_10006");                break;            }            //校验活动时间            $activity = Coupons::getNewUserRegister(Config::get('c_config.c_invite_register_activity_id'),$response);            $activity2 = Coupons::getNewUserRegister(Config::get('config.activity_id'),$response);            if(is_null($activity)){                break;            }            DB::beginTransaction();            $wechat = Users::getUser(Input::get('phone'));            if (is_null($wechat)) {                try{                    $users = new Users();                    $users->phone = Input::get('phone');                    $users->status = 1;                    $users->register_source_id = Input::get('register_source_id',1);                    $users->real_name = '';                    $users->sex = 0;                    $users->area = '010';                    $users->type = 1;                    $users->nickname = '';                    $users->heading_img = '';                    $users->member = Config::get('user.def_member');                    $users->activate = 1;                    $users->save();                    $user_token = new UserTokens;                    $user_token->user_id = $users->id;                    $user_token->token = md5(Input::get('phone').time().rand(0,100));                    $user_token->created_at = Common::num2DateTime(time());                    $user_token->out_time_at = Common::num2DateTime(time() + Config::get("user.token_time_out"));                    $user_token->save();                    $token=$user_token->token;                    $invitation_relation = Users::getUserInviteRelation(Input::get('invite_id'),$users->id);                    if(is_null($invitation_relation)){                        $user_invitation = new UserInvitation();                        $user_invitation->user_id = Input::get('invite_id');                        $user_invitation->invite_user_id = $users->id;                        $user_invitation->save();                        $coupon = true;                        foreach ($activity as  $v) {                            $coupon_base_info = Coupons::getCouponBaseInfo($v->coupon_base_info_id);                            if (!$coupon_base_info) {                                $coupon = false;                                $response = Lang::get('error.coupon_not_exit_error_51004');                            }                            if ($coupon_base_info->status == 0) {                                $coupon = false;                                $response = Lang::get("error.coupon_status_error_51005");                            }                            if ($coupon_base_info->date_type == 0) { //起止时间                                if (strtotime($coupon_base_info->end_date.' 23:59:59') < strtotime(date('Y-m-d H:i:s', time()))) {                                    $coupon = false;                                    $response = Lang::get('error.coupon_game_over_error_51006');                                }                            }                            $coupon_whether_exist = Coupons::getCouponOther($v->coupon_base_info_id);                            if (is_null($coupon_whether_exist)) {                                $coupon = false;                                $response = Lang::get("error.coupon_has_been_brought_out_51008");                            }                            $coupons = Coupons::find($coupon_whether_exist->id);                            $coupons->status = 2;                            $coupons->received_user_id = $users->id;                            $coupons->get_time = date("Y-m-d H:i:s", time());                            if ($coupon_base_info->date_type == 1) { //顺延日期                                $coupons->end_date = date("Y-m-d", strtotime("+" . $coupon_base_info->date_num . " day"));                            }                            $coupons->save();                        }                        if($coupon){                            if(!is_null($activity2)){                                foreach ($activity2 as  $v) {                                    $coupon_base_info = Coupons::getCouponBaseInfo($v->coupon_base_info_id);                                    if (!$coupon_base_info) {                                        continue;                                    }                                    if ($coupon_base_info->status == 0) {                                        continue;                                    }                                    if ($coupon_base_info->date_type == 0) { //起止时间                                        if (strtotime($coupon_base_info->end_date.' 23:59:59') < strtotime(date('Y-m-d H:i:s', time()))) {                                            continue;                                        }                                    }                                    $coupon_whether_exist = Coupons::getCouponOther($v->coupon_base_info_id);                                    if (is_null($coupon_whether_exist)) {                                        continue;                                    }                                    $coupons = Coupons::find($coupon_whether_exist->id);                                    $coupons->status = 2;                                    $coupons->received_user_id = $users->id;                                    $coupons->get_time = date("Y-m-d H:i:s", time());                                    if ($coupon_base_info->date_type == 1) { //顺延日期                                        $coupons->end_date = date("Y-m-d", strtotime("+" . $coupon_base_info->date_num . " day"));                                    }                                    $coupons->save();;                                }                            }                            DB::commit();                            $response = array(                                'code' => 0,                                'msg' => 'ok',                                'result' =>array(                                    'id'=> $users->id,                                    'token' => $token,                                    'phone' => $users->phone,                                    'real_name' => $users->real_name,                                    'sex' => $users->sex,                                    'area' => $users->area,                                    'remark' => $users->remark,                                    'type' => $users->type,                                    'nickname' => $users->nickname,                                    'birthday' => $users->birthday,                                    'heading_img' => $users->heading_img,                                )                            );                        }else{                            break;                        }                    }                }catch (Exception $e){                    DB::rollBack();                    Log::error("error", array("system" => "wechat2.0" ,"event" => "inviteRegister", 'input' =>Input::all() ,"exception" => $e));                    $response = Lang::get("error.data_save_error_30001");                    break;                }            }else{                $response = Lang::get('error.coupon_user_exist_error_71004');                break;            }        }while(false);        Log::info("info", array("system" => "wechat2.0" ,"event" => "inviteRegister", 'input' =>Input::all() ,"output" => $response));        return json_encode($response);    }}